﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNI;

namespace KinectManager
{
    class BodyManager
    {
        private UserGenerator userGenerator;
        private SkeletonCapability skeletonCap;
        private PoseDetectionCapability poseDetectionCap;
        private String calibPose;
        private int nbUsers = 0;
        private bool userDetected = false;
        int userID; 

        public BodyManager(Context context)
        {
            this.userGenerator = new UserGenerator(context);
            this.skeletonCap = this.userGenerator.SkeletonCapability;
            this.poseDetectionCap = this.userGenerator.PoseDetectionCapability;
            this.calibPose = this.skeletonCap.CalibrationPose;

            this.userGenerator.NewUser += userGenerator_NewUser;
            this.userGenerator.LostUser += userGenerator_LostUser;
            this.poseDetectionCap.PoseDetected += poseDetectionCapability_PoseDetected;
            this.skeletonCap.CalibrationComplete += skeletonCapbility_CalibrationComplete;

            this.skeletonCap.SetSkeletonProfile(SkeletonProfile.HeadAndHands);
            //this.skeletonCap.SetSkeletonProfile(SkeletonProfile.Upper);
            this.skeletonCap.SetSmoothing((float)0.5);
        }

        #region public methods
        public void startGenerating()
        {
            this.userGenerator.StartGenerating();
        }

        public void stopGenerating()
        {
            this.userGenerator.StopGenerating();
        }

        public int getNbUsers()
        {
            return this.nbUsers;
        }

        public SkeletonJointPosition GetJoint(int user, SkeletonJoint joint, DepthGenerator depth)
        {
            if (userID != -1)
            {
                SkeletonJointPosition pos = this.skeletonCap.GetSkeletonJointPosition(user, joint);
                if (pos.Position.Z == 0)
                {
                    pos.Confidence = 0;
                }
                else
                {
                    pos.Position = depth.ConvertRealWorldToProjective(pos.Position);
                }
                return pos;
            }
            else
            {
                SkeletonJointPosition pos  = new SkeletonJointPosition();
                pos.Confidence = 0;
                return pos;
            }
        }

        public SkeletonJointPosition GetUserJoint(SkeletonJoint joint, DepthGenerator depth)
        {
            SkeletonJointPosition pos = this.skeletonCap.GetSkeletonJointPosition(userID, joint);
            if (pos.Position.Z == 0)
            {
                pos.Confidence = 0;
            }
            else
            {
                pos.Position = depth.ConvertRealWorldToProjective(pos.Position);
            }
            return pos;
        }
        #endregion

        #region private methods
        private void skeletonCapbility_CalibrationComplete(object sender, CalibrationProgressEventArgs e)
        {
            if (e.Status == CalibrationStatus.OK)
            {
                Console.WriteLine("Calibration complete");
                this.skeletonCap.StartTracking(e.ID);
                this.nbUsers = 1;
                this.userDetected = true;
                this.userID = e.ID;
            }
            else
            {
                this.poseDetectionCap.StartPoseDetection(calibPose, e.ID);
            }
        }


        private void poseDetectionCapability_PoseDetected(object sender, PoseDetectedEventArgs e)
        {
            this.poseDetectionCap.StopPoseDetection(e.ID);
            this.skeletonCap.RequestCalibration(e.ID, true);
        }

        private void userGenerator_NewUser(object sender, NewUserEventArgs e)
        {
            /*if (!userDetected)
            {
                this.skeletonCap.StartTracking(e.ID);
                this.nbUsers++;
                Console.WriteLine("New user");
                this.userDetected = true;
                this.userID = e.ID;
            }*/
            if (!userDetected) this.poseDetectionCap.StartPoseDetection(this.calibPose, e.ID);
        }

        private void userGenerator_LostUser(object sender, UserLostEventArgs e)
        {
            Console.WriteLine("User lost");
            this.nbUsers = 0;
            this.userDetected = false;
            this.userID = -1;
        }
        #endregion
    }
}
